{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "Application using altimetry data obtained from AVISO.\n",
    "* Region of interest: Mediterranean Sea.\n",
    "* Period of interest: May 2014.\n",
    "* Satellite avaible: AltiKa, Cryosat, H2 and JASON-2. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\u001b[1m\u001b[36mINFO: \u001b[39m\u001b[22m\u001b[36mRecompiling stale cache file /home/ctroupin/.julia/v0.6/lib/v0.6/NetCDF.ji for module NetCDF.\n",
      "\u001b[39m"
     ]
    }
   ],
   "source": [
    "using divand\n",
    "using NetCDF\n",
    "using PyPlot\n",
    "using Logging\n",
    "Logging.configure(level=DEBUG);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# User inputs\n",
    "\n",
    "## Domain of interest, resolution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "lonmin, lonmax, latmin, latmax = -7., 40., 30., 48.\n",
    "dx, dy, dt = .125, .125, 1.\n",
    "lonr = lonmin:dx:lonmax;\n",
    "latr = latmin:dy:latmax;\n",
    "timer = 23520:dt:23521;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## File and directories"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "datadir = joinpath(\"../data/\");\n",
    "datafile = joinpath(datadir, \"ADT_20140515_20140604.dat\")\n",
    "resultdir = joinpath(\"../results/divand/\");\n",
    "resultfile = joinpath(resultdir, \"ADT_20140515_20140604_0pt125d1.nc\");\n",
    "info(\"Data file:\\n\", datafile)\n",
    "info(\"Result file:\\n\", resultfile)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Read data file (4-column, ascii)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "20-juil. 10:42:06:INFO:root:Data read from file ../data/ADT_20140515_20140604.dat\n"
     ]
    }
   ],
   "source": [
    "function readAltimetryFile(fname)\n",
    "\n",
    "    data = readlines(open(fname,\"r\"))\n",
    "    nobs = length(data)\n",
    "\n",
    "    lon = zeros(nobs)\n",
    "    lat = zeros(nobs)\n",
    "    time = zeros(nobs)\n",
    "    field = zeros(nobs)\n",
    "\n",
    "    for i in 1:nobs\n",
    "        rec = split(data[i])\n",
    "        lon[i] = parse(Float64,rec[1])\n",
    "        lat[i] = parse(Float64,rec[2])\n",
    "        time[i] = parse(Float64,rec[3])\n",
    "        field[i] = parse(Float64,rec[4])\n",
    "    end\n",
    "    \n",
    "    return lon, lat, time, field\n",
    "end\n",
    "info(\"Data read from file \", datafile)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "20-juil. 10:42:06:INFO:root:Reading 11371 data points\n",
      "20-juil. 10:42:06:INFO:root:Mean field value = -0.09550065957259697\n"
     ]
    }
   ],
   "source": [
    "lon, lat, time, field = readAltimetryFile(datafile);\n",
    "info(\"Reading \", length(lon), \" data points\")\n",
    "info(\"Mean field value = \", mean(field))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Create mask and metrics using coordinate vectors"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "mask, (px, py, pt), (xi, yi, ti) = divand_rectdom(lonr, latr, timer);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(377, 145, 2)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "size(mask)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Analysis parameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# correlation length\n",
    "len = (1., 1., 4.);\n",
    "# obs. error variance normalized by the background error variance\n",
    "epsilon2 = 1.;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Perform interpolation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 21.470195 seconds (19.65 M allocations: 4.255 GiB, 10.04% gc time)\n"
     ]
    }
   ],
   "source": [
    "@time fi,s = divandrun(mask, (px, py, pt), (xi, yi, ti), (lon, lat, time), field, len, epsilon2; alphabc=2);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Export results to netCDF"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "20-juil. 10:44:20:INFO:root:Writing interpolated field in file:\n",
      "../results/divand/ADT_20140515_20140604_0pt125d1.nc\n"
     ]
    },
    {
     "ename": "LoadError",
     "evalue": "\u001b[91mNetCDF error code 13:\n\tPermission denied\u001b[39m",
     "output_type": "error",
     "traceback": [
      "\u001b[91mNetCDF error code 13:\n\tPermission denied\u001b[39m",
      "",
      "Stacktrace:",
      " [1] \u001b[1mcheck\u001b[22m\u001b[22m at \u001b[1m/home/ctroupin/.julia/v0.6/NetCDF/src/netcdf_helpers.jl:22\u001b[22m\u001b[22m [inlined]",
      " [2] \u001b[1mnc_create\u001b[22m\u001b[22m\u001b[1m(\u001b[22m\u001b[22m::String, ::UInt16, ::Array{Int32,1}\u001b[1m)\u001b[22m\u001b[22m at \u001b[1m/home/ctroupin/.julia/v0.6/NetCDF/src/netcdf_c.jl:209\u001b[22m\u001b[22m",
      " [3] \u001b[1mnc_create\u001b[22m\u001b[22m at \u001b[1m/home/ctroupin/.julia/v0.6/NetCDF/src/netcdf_helpers.jl:94\u001b[22m\u001b[22m [inlined]",
      " [4] \u001b[1m#create#35\u001b[22m\u001b[22m\u001b[1m(\u001b[22m\u001b[22m::Dict{Any,Any}, ::UInt16, ::Function, ::String, ::Array{NetCDF.NcVar,1}\u001b[1m)\u001b[22m\u001b[22m at \u001b[1m/home/ctroupin/.julia/v0.6/NetCDF/src/NetCDF.jl:564\u001b[22m\u001b[22m",
      " [5] \u001b[1m(::NetCDF.#kw##create)\u001b[22m\u001b[22m\u001b[1m(\u001b[22m\u001b[22m::Array{Any,1}, ::NetCDF.#create, ::String, ::Array{NetCDF.NcVar,1}\u001b[1m)\u001b[22m\u001b[22m at \u001b[1m./<missing>:0\u001b[22m\u001b[22m",
      " [6] \u001b[1m#create#36\u001b[22m\u001b[22m\u001b[1m(\u001b[22m\u001b[22m::Dict{Any,Any}, ::UInt16, ::Function, ::String, ::NetCDF.NcVar{Float64,3,6}, ::Vararg{NetCDF.NcVar{Float64,3,6},N} where N\u001b[1m)\u001b[22m\u001b[22m at \u001b[1m/home/ctroupin/.julia/v0.6/NetCDF/src/NetCDF.jl:612\u001b[22m\u001b[22m",
      " [7] \u001b[1mcreate\u001b[22m\u001b[22m\u001b[1m(\u001b[22m\u001b[22m::String, ::NetCDF.NcVar{Float64,3,6}\u001b[1m)\u001b[22m\u001b[22m at \u001b[1m/home/ctroupin/.julia/v0.6/NetCDF/src/NetCDF.jl:612\u001b[22m\u001b[22m"
     ]
    }
   ],
   "source": [
    "info(\"Writing interpolated field in file:\\n\", resultfile)\n",
    "sz = size(mask)\n",
    "dims = [NcDim(\"longitude\",sz[1]), NcDim(\"latitude\",sz[2]), NcDim(\"time\",sz[3])];\n",
    "nc = NetCDF.create(resultfile, NcVar(\"ADT\", dims))\n",
    "nc[\"ADT\"][:,:, :] = fi\n",
    "NetCDF.close(nc);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Plotting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Done in [another notebook in Python](../python/plot_AVISO_divand.ipynb) due to a problem with Julia / matplotlib."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Julia 0.6.4",
   "language": "julia",
   "name": "julia-0.6"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "0.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
